// 126
package org.hwadee.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hwadee.entity.Category;
import org.hwadee.entity.Post;
import org.hwadee.service.CategoryService;
import org.hwadee.service.CommentService;
import org.hwadee.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    /**
     * 后台分类列表显示
     *
     * @param page     页码
     * @param pageSize 每页大小
     * @param model    model
     * @return 分类页面
     */
    @RequestMapping(value = "/categoryList")
    public String categoryList(@RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                               Model model) {
        PageHelper.startPage(page, pageSize);
        List<Category> categoryList = categoryService.getAllCategories();
        PageInfo<Category> pageInfo = new PageInfo<Category>(categoryList);
        model.addAttribute("movies", categoryList);
        model.addAttribute("pageInfo", pageInfo);
        return "category";
    }

    /**
     * 后台添加分类提交
     *
     * @param category 分类
     * @return 重定向到分类页面
     */
    @RequestMapping(value = "/insertCategory", method = RequestMethod.POST)
    public String insertCategory(Category category) {
        categoryService.insertCategory(category);
        return "redirect:/category/categoryList";
    }

    /**
     * 删除分类
     *
     * @param category 分类
     * @return 重定向到分类页面
     */
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.POST)
    public String deleteCategory(Category category) {
        categoryService.deleteCategory(category);
        postService.deletePostByCategory(category);
        return "redirect:/category/categoryList";
    }

    /**
     * 分类编辑页面
     * @param id 分类id
     * @param model model
     * @return 分类编辑页面
     */
    @RequestMapping(value = "/editCategoryView")
    public String editCategoryView(@PathVariable("id") Integer id, Model model) {
        Category category = categoryService.getCategoryById(id);
        model.addAttribute("category", category);

        return "editCategoryView";
    }

    /**
     * 编辑分类提交
     * @param category 分类
     * @return 重定向到分类页面
     */
    @RequestMapping(value = "/editCategorySubmit",method = RequestMethod.POST)
    public String editCategorySubmit(Category category) {
        categoryService.updateCategory(category);
        return "redirect:/category/categoryList";
    }

    /**
     * 根据分类查询文章
     *
     * @param categoryId 分类ID
     * @return 模板
     */
    @RequestMapping("/category/{categoryId}")
    public String getArticleListByCategory(@PathVariable("categoryId") Integer categoryId,
                                           @RequestParam(value = "page", defaultValue = "1") int page,
                                           @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                           Model model, HttpSession session) {

        //该分类信息
        Category category = categoryService.getCategoryById(categoryId);
        if (category == null) {
            return "redirect:/Home/Error/404";
        }
        model.addAttribute("category", category);

        //文章列表
        PageHelper.startPage(page, pageSize);
        List<Post> posts = postService.getPostsByCategoryId(categoryId);
        PageInfo<Post> pageInfo = new PageInfo<Post>(posts);
        model.addAttribute("posts", posts);
        model.addAttribute("pageInfo", pageInfo);

        //侧边栏
        //获得随机文章
        List<Post> randomPostList = postService.listRandomPost(8);
        model.addAttribute("randomPostList", randomPostList);
        //获得热评文章
        List<Post> mostCommentPostList = postService.listPostByCommentCount(8);
        model.addAttribute("mostCommentPostList", mostCommentPostList);
        model.addAttribute("pageUrlPrefix", "/category/" + categoryId + "?pageIndex");
        return "Home/Page/postListByCategory";
    }
}
